import flash.events.Event;

internal var mathPI:Number = Math.PI;
internal var mathPIh:Number = Math.PI*.5;
internal var mathPI2:Number = Math.PI*2;

private var _mathRandCount:int;
private var _mathRand:Vector.<Number>;
private var _mathSqrt:Vector.<Number>;

private function mathInit():void{
	var count:int;
	_mathSqrt = new Vector.<Number>( 4000, true );
	_mathRand = new Vector.<Number>( 2000, true );
	
	STAGE.addEventListener( Event.ENTER_FRAME, function( $e:Event ):void{
		var i:int;
		while( i++ < 300 ){
			if( count < 3999 ){
				_mathSqrt[count] = Math.sqrt( count * .001 );
				if ( count < 2000 ){
					_mathRand[count] = Math.random();
				}
				++count;
			}else{
				STAGE.removeEventListener( Event.ENTER_FRAME, arguments.callee );
				break;
			}
		}
	} );
}

public function mathToRadian( $angle:Number ):Number{
	return $angle*0.017453;
}
public function mathToAngle( $radian:Number ):Number{
	return $radian*57.29579;
}
public function mathSqrt( $val:Number ):Number{
	var i:int;
	i = $val*1000;
	return SELF._mathSqrt[i];
}
public function mathRand( $start:int, $end:int ):int{
	var rand:int, max:int;
	
	if( $start === $end ){
		return $start;
	}else if( $start > $end ){
		rand = $start;
		$start = $end;
		$end = rand;
	}
	
	max = $end - $start + 1;
	rand = SELF._mathRand[_mathRandCount++] * max;
	if( SELF._mathRandCount === 2000 ){
		SELF._mathRandCount = 0;
	}
	return $start + rand;
}
public function mathRandN( $start:Number, $end:Number ):Number{
	var rand:Number, max:Number;
	
	if( $start === $end ){
		return $start;
	}else if( $start > $end ){
		rand = $start;
		$start = $end;
		$end = rand;
	}
	
	max = $end - $start;
	rand = SELF._mathRand[_mathRandCount++] * max;
	if( SELF._mathRandCount === 2000 ){
		SELF._mathRandCount = 0;
	}
	return $start + rand;
}
public function mathRandU( $start:uint, $end:uint ):uint{
	var rand:uint, max:uint;
	
	if( $start === $end ){
		return $start;
	}else if( $start > $end ){
		rand = $start;
		$start = $end;
		$end = rand;
	}
	
	max = $end - $start + 1;
	rand = SELF._mathRand[_mathRandCount++] * max;
	if( SELF._mathRandCount === 2000 ){
		SELF._mathRandCount = 0;
	}
	
	return $start + rand;
}
//static public function mathSin( $val:Number ):Number{
//	var i:int;
//	i = $val*1000;
//	return _sin[i];
//}
//static public function mathCos( $val:Number ):Number{
//	var i:int;
//	i = $val*1000;
//	return _cos[i];
//}